package thread;

public class Demo10 {
    private static boolean isRunning = true;
    public static void main(String[] args) {
        //boolean isRunning = true;下面代码isRunning编译不了 变量捕获需要是final或者”事实“fianl由于下方进行了修改所以无法捕获
        Thread t = new Thread(() -> {
            while(isRunning){//此处将不是变量捕获而是内部类访问外部类成员
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
            System.out.println("t线程结束");
        });

        t.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //3s之后修改isRunning的值，从而通知t结束
        System.out.println("控制t线程结束");
        isRunning = false;
    }
}
